//package com.example.springai.http;
//
//import com.example.springai.http.pojo.ChatCompletionResponse;
//import com.example.springai.http.pojo.ChatRequest;
//import com.fasterxml.jackson.annotation.JsonCreator;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.List;
//
//public class AddOpenAiClient {
//    @Value("${spring.ai.openai.base-url}")
//    private static String OPENAI_API_URL="https://api.deepseek.com/v1/chat/completions";
//
//    @Value("${openai.api.api-key}")
//    private static String OPENAI_API_KEY="sk-ec1956b29ea14537ac7b609056752ef9";
//
//    public static void main(String[] args) throws JsonProcessingException {
//        RestTemplate restTemplate = new RestTemplate();
//
//        // 设置请求头
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.set("Authorization", "Bearer " + OPENAI_API_KEY);
//
//        // 构建请求体
//        ChatRequest chatRequest = new ChatRequest();
//        chatRequest.setModel("deepseek-chat");
//
//        // 设置消息
//        ChatRequest.Message message = new ChatRequest.Message();
//        message.setRole("user");
//        message.setContent("西安今天的天气怎么样");
//        chatRequest.setMessages(Arrays.asList(message));
//
//        // 设置工具
//        ChatRequest.Tool tool = new ChatRequest.Tool();
//        tool.setType("function");
//
//        ChatRequest.Tool.Function function = new ChatRequest.Tool.Function();
//        function.setName("addSchool");
//        function.setDescription("添加学校信息");
//
//        ChatRequest.Tool.Function.Parameters parameters = new ChatRequest.Tool.Function.Parameters();
//        parameters.setType("object");
//
//        ChatRequest.Tool.Function.Parameters.Properties properties = new ChatRequest.Tool.Function.Parameters.Properties();
//
//        ChatRequest.Tool.Function.Parameters.Properties.Location location = new ChatRequest.Tool.Function.Parameters.Properties.Location();
//        location.setType("string");
//        location.setDescription("城市姓名，例如西安,北京,上海");
//        properties.setLocation(location);
//
//        ChatRequest.Tool.Function.Parameters.Properties.Unit unit = new ChatRequest.Tool.Function.Parameters.Properties.Unit();
//        unit.setType("string");
//
//        unit.setEnumValues(Arrays.asList("C", "F"));
//        properties.setUnit(unit);
//
//        parameters.setProperties(properties);
//        parameters.setRequired(Arrays.asList("location"));
//
//        function.setParameters(parameters);
//        tool.setFunction(function);
//
//        chatRequest.setTools(Arrays.asList(tool));
//        chatRequest.setToolChoice("auto");
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = objectMapper.writeValueAsString(chatRequest);
////        System.out.println(json);
//        System.out.println("chatRequest = " + json);
//
//        // 创建Http实体
//        HttpEntity<ChatRequest> entity = new HttpEntity<>(chatRequest, headers);
//
//        // 发送请求并获取响应
//        String response = restTemplate.postForObject(OPENAI_API_URL, entity, String.class);
////        ChatRequest chatRequest = objectMapper.readValue(json, ChatRequest.class);
//        System.out.println("response="+response);
//        List<ChatRequest.Message> messages = chatRequest.getMessages();
//        List<ChatRequest.Message> mutableMessages = new ArrayList<>(messages);
//        ObjectMapper objectMapper1 = new ObjectMapper();
//        ChatCompletionResponse response1 = objectMapper1.readValue(response, ChatCompletionResponse.class);
//// 添加新的消息
//        ChatRequest.Message toolMessage = new ChatRequest.Message();
//        toolMessage.setRole("assistant");
//        toolMessage.setTool_call_id(response1.getChoices().get(0).getMessage().getToolCalls().get(0).getId());
//        toolMessage.setContent("准备获取天气数据");
//        mutableMessages.add(toolMessage);
//
//        ChatRequest.Message tool1Message = new ChatRequest.Message();
//        tool1Message.setRole("tool");
//        tool1Message.setTool_call_id(response1.getChoices().get(0).getMessage().getToolCalls().get(0).getId());
//        tool1Message.setContent("15度");
//        mutableMessages.add(tool1Message);
//        chatRequest.setMessages(mutableMessages);
//        chatRequest.setTool_calls(response1.getChoices().get(0).getMessage().getToolCalls());
//        HttpEntity<ChatRequest> messageEntity =  new HttpEntity<>(chatRequest, headers);
//        ObjectMapper argumentsMapper =  new ObjectMapper();
//        String s1 = argumentsMapper.writeValueAsString(messageEntity);
//        System.out.println("s1 = " + s1);
//        String s = restTemplate.postForObject(OPENAI_API_URL, messageEntity, String.class);
//        System.out.println("s = " + s);
//        // 输出响应
//
//
//
//
//        System.out.println("response1 = " + response1);
//
//    }
//
//    private static String handleToolCall(ChatCompletionResponse.Function function) throws JsonProcessingException {
//        String arguments = function.getArguments();
//        ObjectMapper argumentsMapper =  new ObjectMapper();
//        HashMap hashMap = argumentsMapper.readValue(arguments, HashMap.class);
//        Object location = hashMap.get("location");
//
//        // 模拟工具调用的响应逻辑
//        return location.toString()+"15度";
//    }
//
//}
